<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tallgy.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="JavaScript 模块化​        我们可以知道，JavaScript原来是应用于浏览器端的，所以当时是没有模块化的概念的。 ​        在模块化之前，JavaScript主要使用闭包和立即执行函数来将方法用命名空间来进行划分。 ​        但是后来Nodejs的诞生，将JavaScript带入了服务端之后，模块化就变得不可或缺。此时CommonJS便由此而生了。 模块化：">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript模块化">
<meta property="og:url" content="http://tallgy.gitee.io/2022/03/08/JavaScript/%E9%9A%8F%E7%AC%94/JavaScript%E6%A8%A1%E5%9D%97%E5%8C%96/index.html">
<meta property="og:site_name" content="tallgy&#39;s blog">
<meta property="og:description" content="JavaScript 模块化​        我们可以知道，JavaScript原来是应用于浏览器端的，所以当时是没有模块化的概念的。 ​        在模块化之前，JavaScript主要使用闭包和立即执行函数来将方法用命名空间来进行划分。 ​        但是后来Nodejs的诞生，将JavaScript带入了服务端之后，模块化就变得不可或缺。此时CommonJS便由此而生了。 模块化：">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://tallgy.gitee.io/.io//nodejs-require.jpg">
<meta property="article:published_time" content="2022-03-08T14:21:48.000Z">
<meta property="article:modified_time" content="2023-06-25T08:57:29.338Z">
<meta property="article:author" content="tallgy">
<meta property="article:tag" content="随笔">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://tallgy.gitee.io/.io//nodejs-require.jpg">


<link rel="canonical" href="http://tallgy.gitee.io/2022/03/08/JavaScript/%E9%9A%8F%E7%AC%94/JavaScript%E6%A8%A1%E5%9D%97%E5%8C%96/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://tallgy.gitee.io/2022/03/08/JavaScript/%E9%9A%8F%E7%AC%94/JavaScript%E6%A8%A1%E5%9D%97%E5%8C%96/","path":"2022/03/08/JavaScript/随笔/JavaScript模块化/","title":"JavaScript模块化"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>JavaScript模块化 | tallgy's blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">tallgy's blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#JavaScript-%E6%A8%A1%E5%9D%97%E5%8C%96"><span class="nav-number">1.</span> <span class="nav-text">JavaScript 模块化</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Nodejs-%E7%9A%84-CommonJS"><span class="nav-number">1.1.</span> <span class="nav-text">Nodejs 的 CommonJS</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#AMD%E3%80%81CMD"><span class="nav-number">1.2.</span> <span class="nav-text">AMD、CMD</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ES6"><span class="nav-number">1.3.</span> <span class="nav-text">ES6</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tallgy</p>
  <div class="site-description" itemprop="description">只是一个知识的搬运工</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">107</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2022/03/08/JavaScript/%E9%9A%8F%E7%AC%94/JavaScript%E6%A8%A1%E5%9D%97%E5%8C%96/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JavaScript模块化
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-03-08 22:21:48" itemprop="dateCreated datePublished" datetime="2022-03-08T22:21:48+08:00">2022-03-08</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/JavaScript/" itemprop="url" rel="index"><span itemprop="name">JavaScript</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/JavaScript/%E9%9A%8F%E7%AC%94/" itemprop="url" rel="index"><span itemprop="name">随笔</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="JavaScript-模块化"><a href="#JavaScript-模块化" class="headerlink" title="JavaScript 模块化"></a>JavaScript 模块化</h1><p>​        我们可以知道，JavaScript原来是应用于浏览器端的，所以当时是没有模块化的概念的。</p>
<p>​        在模块化之前，JavaScript主要使用闭包和立即执行函数来将方法用命名空间来进行划分。</p>
<p>​        但是后来Nodejs的诞生，将JavaScript带入了服务端之后，模块化就变得不可或缺。此时CommonJS便由此而生了。</p>
<p>模块化：</p>
<ul>
<li>后端：CommonJS 对应的 Node.js</li>
<li>前端：AMD 对应的 RequireJS  和 CMD 对应的 Sea.js</li>
<li>ES6：ES6 的模块化规范。</li>
</ul>
<h2 id="Nodejs-的-CommonJS"><a href="#Nodejs-的-CommonJS" class="headerlink" title="Nodejs 的 CommonJS"></a>Nodejs 的 CommonJS</h2><p>​    Nodejs 参照了 CommonJS 的规范，通过 require 和 exports 进行模块化。</p>
<p>​        关键词：require、exports、module.exports</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">简单的使用</span><br><span class="line">const depend = require(&#x27;../xxx&#x27;)</span><br><span class="line"></span><br><span class="line">module.exports.run = 1;</span><br><span class="line"></span><br><span class="line">这里：我们省略了后缀，node将按照一下顺序进行查找。其中，如果x是包模块，那么会查找 package.json 的main属性</span><br><span class="line">X.js</span><br><span class="line">X.json</span><br><span class="line">X.node</span><br><span class="line">X/index.js</span><br><span class="line">X/index.json</span><br><span class="line">X/index.node</span><br></pre></td></tr></table></figure>

<p>​    require 就是导入。</p>
<p>​    exports 就是导出的接口，是 module.exports 的一个引用，但是我们最终使用的时候还是使用的 module.exports。</p>
<p>​    所以，如果我们对 exports 的修改是一个对象的赋值，那么会导致exports会指向另外一个栈的地址，但是并不会修改到 model.exports。</p>
<p>​    所以如果我们要导出一个对象，那么我们就需要使用 module.exports，这样的修改才能造成影响。</p>
<p>​    CommonJS 的模块化，导出和缓存的步骤。</p>
<p><img src="/.io//nodejs-require.jpg" alt="nodejs-require"></p>
<p>​        我们会先判断引入的文件是否存在缓存区，同时判断是否是原生的模块，对于原生模块，回去判断是否在原生模块的缓存。然后最终就是加载和引入。</p>
<h2 id="AMD、CMD"><a href="#AMD、CMD" class="headerlink" title="AMD、CMD"></a>AMD、CMD</h2><p>AMD规范：RequireJS</p>
<p>​    AMD规范是异步的，通过define方法将代码定义为模块，在被require时加载依赖的模块。</p>
<p>​    AMD 是 “Asynchronous Module Definition” 的缩写，意思就是”异步模块定义”。</p>
<p>CMD规范：Sea.js</p>
<p>​        AMD和CMD的规范其实都差不多。只是AMD的一个异步的CMD是一个在书写上依照了CommonJS的一个规范。</p>
<h2 id="ES6"><a href="#ES6" class="headerlink" title="ES6"></a>ES6</h2><p>​        ES6模块化，Es6在后续也出现了模块化，就是</p>
<p>import，exports</p>
<ul>
<li>和commonjs的区别：</li>
</ul>
<p>​        CommonJS 只能在运行时确定导出的接口，实际导出的就是一个对象。而 ES6 Module 的设计思想是尽量的静态化，使得编译时就能确定模块的依赖关系，以及导入和导出的变量，也就是所谓的”编译时加载”。</p>
<p>​        正因为如此，<code>import</code> 命令具有提升效果，会提升到整个模块的头部，首先执行。</p>
<p>​        简单来说就是，我虽然没有先import，但是我还是会提前import。就像是function写在下面也可以调用一样。</p>
<p>​        也正因为 ES6 Module 是编译时加载， 所以不能使用表达式和变量，因为这些是只有在运行时才能得到结果的语法结构。</p>
<p>​        就是，不存在变量的使用情况，因为此时还没有。</p>
<p>​        commonjs 是深拷贝，而es6是浅拷贝</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E9%9A%8F%E7%AC%94/" rel="tag"># 随笔</a>
              <a href="/tags/JavaScript/" rel="tag"># JavaScript</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/03/05/JavaScript/ES6/JavaScript-ES6/" rel="prev" title="JavaScript-ES6">
                  <i class="fa fa-chevron-left"></i> JavaScript-ES6
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/03/12/%E6%B5%8F%E8%A7%88%E5%99%A8/SEO%E5%8E%9F%E7%90%86/" rel="next" title="SEO原理">
                  SEO原理 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tallgy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
